package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.IFacebookDao;
import com.cn.wanxi.dao.impl.FacebookDaoImpl;
import com.cn.wanxi.model.FacebookModel;
import com.cn.wanxi.model.PageModel;
import com.cn.wanxi.service.IFacebookService;
import redis.clients.jedis.Jedis;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FacebookServiceImpl implements IFacebookService {
    IFacebookDao facebookDao = new FacebookDaoImpl();
    @Override
    public int deleteFacebook(int id) {
        return facebookDao.deleteFacebook(id);
    }

    @Override
    public List<FacebookModel> getFacebookList() {
        Jedis jedis = new Jedis();
        List<FacebookModel> facebookList = null;
        try {
            if (jedis.exists("facebook_id")){
                facebookList = facebookDao.getFacebookListByRedis();
            }else {
                facebookList = facebookDao.getFacebookList();
                facebookDao.setFacebookInRedis(facebookList);
            }

        }catch (Exception e){
            facebookList = facebookDao.getFacebookList();
        }
        return facebookList;
    }

    @Override
    public FacebookModel getFacebook(int id) {
        return facebookDao.getFacebook(id);
    }

    @Override
    public PageModel<FacebookModel> getFacebooks(Map<String,Object> map) {
        PageModel pageModel = new PageModel();
        pageModel.setList(facebookDao.getFacebooks(map));
        pageModel.setTotal(facebookDao.getFacebookCount(map));
        return pageModel;
    }

    @Override
    public int addFacebook(FacebookModel facebookModel) {
        return facebookDao.addFacebook(facebookModel);
    }
}
